
public class MyLinklist {
    class ListNode {
        int val;
        ListNode prev;
        ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;
    public ListNode last;

    public void display(){
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
    }

    public int size(){
        int count = 0;
        ListNode cur = head;
        while(cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    public void addFirst(int data){
        ListNode node = new ListNode(data);
        if(head == null){
            head = node;
            last = node;
            return;
        }
        node.next = head;
        head.prev = node;
        head = node;
    }
    public void addLast(int data){
        ListNode node = new ListNode(data);
        if(head == null){
            head = node;
            last = node;
            return;
        }
        last.next = node;
        node.prev = last;
        last = node;
    }

    private ListNode SearchIndex(int index) {
        ListNode cur = head;
        while(index != 0){
            cur = cur.next;
            index--;
        }
        return cur;
    }

    public void addIndex(int index,int data){
        if(index < 0 || index > size()){
            System.out.println("下标不合法");
            return;
        }
        ListNode node = new ListNode(data);
        ListNode cur = SearchIndex(index);
        node.next = cur.prev.next;
        node.prev = cur.prev;
        cur.prev.next = node;
        cur.prev = node;
    }

    public boolean contains(int key){
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    public void remove(int key) {
        ListNode cur = head;
        while (cur != null){
            if(cur.val == key){
                if(cur == head) {
                    cur = cur.next;
                    cur.prev = null;
                    head = cur;
                    if (last == head) {
                        last = null;
                    }
                }else{
                    cur.prev.next = cur.next;
                    if(cur.next == null){
                        cur.prev.next=null;
                        last = cur.prev;
                    }else {
                        cur.next.prev = cur.prev;
                    }
                }
                return;
            }else{
                cur = cur.next;
            }
        }
    }
    public void removeAllKey(int key){
        ListNode cur = head;
        while (cur != null){
            if(cur.val == key){
                if(cur == head) {
                    if(cur.next == null){
                        head=null;
                        last=null;
                    }else{
                        head = head.next;
                        head.prev = null;

                    }
                }else{
                    cur.prev.next = cur.next;
                    if(cur.next == null){
                        cur.prev.next=null;
                        last = cur.prev;
                    }else {
                        cur.next.prev = cur.prev;
                    }
                }

            }
            cur = cur.next;
        }
    }

    public void clear(){
        ListNode cur = head;
        while(cur!=null){
            ListNode curnext = cur.next;
            cur.prev=null;
            cur.next=null;
            head=cur;
            cur=curnext;
        }
        last=null;
    }
}
